package com.guide.dao.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.guide.dao.MessageDao;
import com.guide.domain.Message;
import com.guide.domain.MessageQuery;
import com.guide.domain.MessageQuery.Criteria;
import com.guide.persistence.MessageMapper;
@Repository("messageDao")
public class MessageDaoImpl implements MessageDao {
	private static final Logger logger = LoggerFactory
			.getLogger(MessageDaoImpl.class);


	@Autowired
	private MessageMapper  messageMapper;


	@Override
	public int insertMessage(Message message) {
		// TODO Auto-generated method stub
		return messageMapper.insert(message);
	}


	@Override
	public List<Message> getMessageByPage(Long userId, Integer pageIndex,
			Integer limit) {
		// TODO Auto-generated method stub
		MessageQuery query = new MessageQuery();
		query.createCriteria().andRecIdEqualTo(userId);
		query.setOrderByClause("ctime desc");
		return messageMapper.selectByExample(query, pageIndex*limit, limit);
	}


	@Override
	public int counrMessageByUserId(Long userId) {
		// TODO Auto-generated method stub
		MessageQuery query = new MessageQuery();
		query.createCriteria().andRecIdEqualTo(userId);
		return messageMapper.countByExample(query);
	}


	@Override
	public Message getMessageById(Long messageId) {
		// TODO Auto-generated method stub
		return messageMapper.selectByPrimaryKey(messageId);
	}


	@Override
	public void markRead(Message massage) {
		// TODO Auto-generated method stub
		messageMapper.updateByPrimaryKeySelective(massage);
	}


	@Override
	public Integer getUnreadLetterCount(Long userId,Integer status) {
		// TODO Auto-generated method stub
		MessageQuery query = new MessageQuery();
		query.createCriteria().andRecIdEqualTo(userId).andStatusEqualTo(status);
		return messageMapper.countByExample(query);
	}


	@Override
	public List<Message> getMessageSysByPage(Integer msgType,Integer pageIndex, Integer pageSize) {
		// TODO Auto-generated method stub
		MessageQuery query = new MessageQuery();
		Criteria criteria = query.createCriteria().andRecIdEqualTo(-1L);
		if(msgType!=-1){
			criteria.andMsgtypeEqualTo(msgType);
		}
		return messageMapper.selectByExample(query, pageIndex*pageSize, pageSize);
	}


	@Override
	public int countMessageSysByUserId(Integer msgType) {
		// TODO Auto-generated method stub
		MessageQuery query = new MessageQuery();
		Criteria criteria = query.createCriteria().andRecIdEqualTo(-1L);
		if(msgType!=-1){
			criteria.andMsgtypeEqualTo(msgType);
		}
		return messageMapper.countByExample(query);
	}

	
	



}
